package result

// 简单返回结果
type SimpleR struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// 通用返回结果
type R struct {
	SimpleR
	Data interface{} `json:"data"`
}

func NewSimpleR(code int, msg string) *SimpleR {
	return &SimpleR{Code: code, Msg: msg}
}

var (
	// 未登录返回结果
	R_NOT_LOGIN = NewSimpleR(UNLOGIN_CODE, UNLOGIN_MSG)
	// 操作成功返回
	R_SUCCESS = NewSimpleR(SUCCESS_CODE, SUCCESS_MSG)
	// 未找到资源返回
	R_NOT_FOUND = NewSimpleR(NOT_FOUND_CODE, NOT_FOUND_MSG)
	// 未支持的方法返回
	R_NOT_SUPPORT_METHOD = NewSimpleR(NOT_SUPPORT_METHOD_CODE, NOT_SUPPORT_METHOD_MSG)
)

// 设置数据
func (r *R) SetData(i interface{}) *R {
	r.Data = i
	return r
}

// 设置消息
func (r *R) SetMsg(s string) *R {
	r.Msg = s
	return r
}

// 设置编号
func (r *R) SetCode(i int) *R {
	r.Code = i
	return r
}

// 新建一个成功的返回结果
func SuccessR(d interface{}) *R {
	r := &R{
		SimpleR: SimpleR{
			Code: SUCCESS_CODE,
			Msg:  SUCCESS_MSG,
		},
		Data: d,
	}
	return r
}

func NotLoginR() *R {
	r := &R{
		SimpleR: SimpleR{
			Code: UNLOGIN_CODE,
			Msg:  UNLOGIN_MSG,
		},
	}
	return r
}

func NotFoundR() *R {
	r := &R{
		SimpleR: SimpleR{
			Code: NOT_FOUND_CODE,
			Msg:  NOT_FOUND_MSG,
		},
	}
	return r
}

func NotSupportMethodR() *R {
	r := &R{
		SimpleR: SimpleR{
			Code: NOT_SUPPORT_METHOD_CODE,
			Msg:  NOT_SUPPORT_METHOD_MSG,
		},
	}
	return r
}
